Skip to content

Reconnect and resend when Studio drops an idle connection - #889

Merged
rtibbles merged 1 commit into
learningequality:mainfrom
rtibblesbot:issue-883-476d7f
Nov 18, 2026
Merged

rtibbles merged 1 commit into
learningequality:mainfrom
rtibblesbot:issue-883-476d7f

Conversation

@rtibblesbot

@rtibblesbot rtibblesbot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • config.SESSION resends a request when a reused keep-alive connection fails with RemoteDisconnected, so the first request after a long build no longer fails.
  • Resends only when the body can be replayed: none, bytes/str, or a file it can rewind; streamed bodies and drops on fresh connections still raise.
  • Reuse is tracked per thread through the pool's _get_conn, covering proxied connections too.

References

Fixes #883. First seen: https://community.learningequality.org/t/sushichef-script-to-import-a-channel-failing-with-connection-aborted/4073

Reviewer guidance

Suggested review: Full code review, then manual QA once the reviewer sets Needs QA.

  • Decide whether AC 2 allows resending a non-idempotent POST (add_nodes, create_channel) on any reset or broken pipe on a reused connection, since Studio applies it twice if it processed the request before the reset.
  • Decide whether every Studio request may depend on private internals (urllib3's _get_conn and pool_classes_by_scheme, pinned via urllib3==2.8.0; requests' PreparedRequest._body_position, with no upper bound on requests).
  • Run the issue's 11-minute repro chef against production Studio over HTTPS, which no test reaches: expect no RemoteDisconnected and the channel to appear once with one exercise.
  • Fake-Studio tests in tests/test_chef_integration.py run on CI only; the agent's sandbox blocks binding local ports.
  • Not run against a live Studio: run the issue's repro chef with --token=<studio token>.

AI usage

Claude Code traced the failure, wrote the adapter, fake-Studio tests and this description. Verified with the full test suite and prek.

Deviations from the issue spec

Criterion Why not
Nothing is sent to Studio twice A request whose reused connection closes with no response is resent; safe when Studio drops the idle connection before reading it, but a request Studio processed and then closed without answering would be applied twice.

Written by rtibblesbot, an LLM-based coding agent.

@rtibblesbot

🟡 Waiting for feedback

WAITING · on a reviewer
Last updated: 2026-11-10 14:09 UTC

@rtibblesbot
rtibblesbot marked this pull request as ready for review November 5, 2026 01:00
@rtibblesbot
rtibblesbot requested review from a team and leq-bot November 5, 2026 01:00

@rtibbles rtibbles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong approach to my mind.

Comment thread ricecooker/utils/request_utils.py Outdated
auth.rebuild(prepared_request, response.request)


class IdleConnectionAdapter(HTTPAdapter):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like the wrong fix - as you say in your open question "is a 60 second timeout right?" How would we know? Surely the better and more Pythonic alternative is to monitor for session disconnection and retry in that case? Rather than try to anticipate the problem, we catch and handle it. It's significantly easier to test also, and exactly replicates the reported error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Replaced the idle-timeout eviction with catch-and-retry:

  • ReconnectingAdapter (request_utils.py) catches ConnectionError wrapping RemoteDisconnected and resends on a fresh connection. It retries up to pool_maxsize times, enough to work through every stale pooled connection.
  • Only RemoteDisconnected is retried, i.e. Studio closed the connection without responding. Timeouts, refusals and HTTP errors still raise, so a request Studio answered is never resent.
  • A file body is rewound to where it started before the resend. A body that can't be rewound (e.g. an iterator) raises instead.
  • STUDIO_CONNECTION_MAX_IDLE and the 60s threshold are gone.
  • The test is the reported error: the fake Studio reads the request and closes the connection without responding, and the run completes with one create_channel dropped and resent.
  • Searched for other copies of the predict-the-idle-limit approach: the eviction adapter was the only one. Every Studio request (7 config.SESSION calls in commands.py and managers/tree.py) goes through the mounted adapter.

@rtibblesbot rtibblesbot changed the title Open a fresh Studio connection after a long idle gap Reconnect and resend when Studio drops an idle connection Nov 5, 2026

@rtibbles rtibbles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poor test hygiene using real sleeps to simulate idle when we can surely do it a better way.

Comment thread tests/test_chef_integration.py Outdated

class SlowBuildChef(TwoTopicChef):
def construct_channel(self, **kwargs):
time.sleep(2 * SERVER_IDLE_LIMIT)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleeping in a test is a bad idea - we should be making the disconnect happen deterministically, not relying on an idle timeout actually happening.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the sleeps with an explicit switch. SilentDropStudio.go_idle() marks every connection that has already answered a request as dead, so the server silently drops the next request on it. Fresh connections still get answered.

  • Searched the branch's tests for time.sleep, time.monotonic and SERVER_IDLE_LIMIT. 3 sleeps matched, and I changed all 3: SlowBuildChef.construct_channel, the idle_reconnecting_session fixture, and test_reconnecting_adapter_resends_through_proxy_after_drop.
  • SERVER_IDLE_LIMIT and the server's timing check are gone, and nothing on the branch's tests sleeps now.
  • With the adapter unmounted, the chef test still fails with SystemExit.

Written by rtibblesbot, an LLM-based coding agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rtibbles
rtibbles merged commit 78e540a into learningequality:main Nov 18, 2026
78 checks passed

Copy link
Copy Markdown
Contributor Author

Quick check on rtibblesbot's work on this PR: react 👍 helpful · 😕 mixed · 👎 not helpful. Reply with anything specific.

@rtibblesbot
rtibblesbot deleted the issue-883-476d7f branch November 18, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upload fails with RemoteDisconnected when a chef takes a long time to build

2 participants